//AC
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
 
int version[N]; // version[0]是服务器,记录字符串的版本号

pair<int, string> record[N];
// first : father
//  second :  追加的字符

void dfs(int x) {
  if (!x)
    return;
  int father = record[x].first;
  dfs(father);
  cout << record[x].second; // 返回的时候打印
}

int main() {

  int n, q, idx = 0;
  cin >> n >> q;

  for (int i = 1; i <= q; i++) {
    int op, p;
    cin >> op >> p;
    if (op == 1)
      version[p] = version[0];
    else if (op == 2) {
      string s;
      cin >> s;
      record[++idx] = {version[p], s};
      version[p] = idx;
    } else
      version[0] = version[p];
  }

  dfs(version[0]); 
 

  return 0;
}